package admin

import (
	"duoke/model"
	"duoke/service"
	"duoke/utils/response"
	"duoke/utils/valid"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
)

var SeparateAccountsSub = new(separateAccountsSubCtl)

type separateAccountsSubCtl struct{}

func (ctl *separateAccountsSubCtl) List(c *gin.Context) {
	var req model.ReqList
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	list, total, err := service.SeparateAccountsSub.Page(c, req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "获取成功", gin.H{"total": total, "list": list})
}

func (ctl *separateAccountsSubCtl) Save(c *gin.Context) {
	var req model.ReqSeparateAccountsSub
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	err := service.SeparateAccountsSub.Save(c, req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "保存成功", "")
}

func (ctl *separateAccountsSubCtl) Del(c *gin.Context) {
	var req model.ReqId
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), nil)
		return
	}
	err := service.SeparateAccountsSub.Del(c, req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "删除成功", nil)
}
